﻿//-----------------------------------------------
// RedditSharp (Reddit#)
//
// Copyright (c) 2011 - 2012
// - Angelo Geels <angelo@angelog.nl>
//-----------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedditSharp
{
    public class RedditPost : RedditThing
    {
        public string Title;
        public string Author;
        public string Permalink;

        public int TimePosted;
        public int TimePostedUTC;

        public bool NSFW;
        public bool Self;

        public string Thumbnail;
        public string URL;
        public string Domain;
        public string Selftext;

        private RedditThread _Thread;
        /// <summary>
        /// Gets the RedditThread from this post. If it's not cached, it will download the thread first. Otherwise, it will return the cached version.
        /// </summary>
        public RedditThread Thread
        {
            get
            {
                if (this._Thread == null)
                    this._Thread = Reddit.GetThread(this.ID);
                return this._Thread;
            }
        }

        /// <summary>
        /// Forces a recache on the Thread property.
        /// </summary>
        public void RecacheThread()
        {
            this._Thread = null;
        }

        /// <summary>
        /// Forces a recache on the Thread property and also downloads the thread.
        /// </summary>
        public void ReloadThread()
        {
            this._Thread = Reddit.GetThread(this.ID);
        }

        /// <summary>
        /// Parses a reddit post based on the Hashtable received.
        /// </summary>
        /// <param name="PostData">The post data in Hashtable form</param>
        /// <returns>The RedditPost</returns>
        public static RedditPost Parse(Hashtable PostData)
        {
            RedditPost newPost = new RedditPost();

            newPost.ID = (string)PostData["id"];
            newPost.FullID = (string)PostData["name"];
            newPost.Title = (string)PostData["title"];
            newPost.Author = (string)PostData["author"];
            newPost.Permalink = (string)PostData["permalink"];
            newPost.Subreddit = (string)PostData["subreddit"];

            newPost.TimePosted = (int)(double)PostData["created"];
            newPost.TimePostedUTC = (int)(double)PostData["created_utc"];

            newPost.NSFW = (bool)PostData["over_18"];
            newPost.Self = (bool)PostData["is_self"];

            newPost.MyScore = PostData["likes"] == null ? 0 : (bool)PostData["likes"] == true ? 1 : -1;
            newPost.Score = (int)(double)PostData["score"];
            newPost.Upvotes = (int)(double)PostData["ups"];
            newPost.Downvotes = (int)(double)PostData["downs"];
            newPost.Comments = (int)(double)PostData["num_comments"];

            newPost.Thumbnail = (string)PostData["thumbnail"];
            newPost.URL = (string)PostData["url"];
            newPost.Domain = (string)PostData["domain"];
            newPost.Selftext = (string)PostData["selftext"];

            return newPost;
        }
    }
}
